package com.shuoyue.ycgk.ui.mok;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.Mok;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;

public interface MokDetailContract {
    interface View extends BaseView {
        void setMokInfo(Mok info);

        void registSuc();

        void startSuc(UserTestPaper userTestPaper);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getMokInfo(int id) {
            apply(model.getMokInfo(id))
                    .subscribe(new ApiSubscriber<Optional<Mok>>(mView, this) {
                        @Override
                        public void onNext(Optional<Mok> listOptional) {
                            super.onNext(listOptional);
                            mView.setMokInfo(listOptional.getIncludeNull());
                        }
                    });
        }

        public void registMok(int id) {
            apply(model.registMok(id))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> listOptional) {
                            super.onNext(listOptional);
                            mView.registSuc();
                        }
                    });
        }

        public void startMok(int paperId) {
            apply(model.startMok(paperId))
                    .subscribe(new ApiSubscriber<Optional<UserTestPaper>>(mView, this) {
                        @Override
                        public void onNext(Optional<UserTestPaper> listOptional) {
                            super.onNext(listOptional);
                            mView.startSuc(listOptional.getIncludeNull());
                        }
                    });
        }
    }

    class Model extends BaseModel {
        Observable<BaseResult<Mok>> getMokInfo(int id) {
            return RetrofitClient.getInstance().getApi().getMokInfo(id);
        }

        Observable<BaseResult<String>> registMok(int id) {
            return RetrofitClient.getInstance().getApi().registMok(id);
        }

        Observable<BaseResult<UserTestPaper>> startMok(int paperId) {
            return RetrofitClient.getInstance().getApi().getMokPaper(paperId);
        }
    }
}
